
<template>
    <div class="spanControl-list" style="width: 100%;margin-left: 0!important;">
        <el-card class="box-card card-head" style="margin: 0px; width: 100%; min-height: 99%">
            <div slot="header" class="clearfix">
                <span>注册审核</span>
            </div>
            <div class="alert-container clearfix">
                <el-alert  v-for="alert in alerts.successes"  :key="alert.title"
                           v-bind:title="alert.title"
                           type="success"
                           show-icon>
                </el-alert>
                <el-alert  v-for="alert in alerts.infos"  :key="alert.title"
                           v-bind:title="alert.title"
                           type="info"
                           show-icon >
                </el-alert>
                <el-alert  v-for="alert in alerts.warnings"  :key="alert.title"
                           v-bind:title="alert.title"
                           type="warning"
                           show-icon >
                </el-alert>
                <el-alert  v-for="alert in alerts.errors"  :key="alert.title"
                           v-bind:title="alert.title"
                           type="error"
                           show-icon >
                </el-alert>
            </div>
            <div class="text item">
                <el-form ref="businessRegistrationForm" :model="businessRegistration" label-width="150px" :rules="rules">
                                    <!--<el-col :span="11">-->
                                        <!--<el-form-item label="名称" prop="name">-->
                                            <!--<file-tip content="名称"></file-tip>-->
                                            <!--<el-input type="input" v-model="businessRegistration.name"-->
                                                      <!--placeholder="名称" clearable autosize-->
                                                      <!--resize ="both" tabindex=1-->
                                                              <!--maxlength=255-->
                                            <!--&gt;</el-input>-->
                                    <!--</el-form-item>-->
                                    <!--</el-col>-->

                  <el-col :span="11">
                    <el-form-item label="商家名称" prop="businessName">
                      <file-tip content="商家名称"></file-tip>
                      <el-input type="input" v-model="businessRegistration.business.businessName"
                                placeholder="商家名称" clearable autosize
                                resize="both" tabindex=4
                                maxlength=30
                                disabled="disabled"
                      ></el-input>
                    </el-form-item>
                  </el-col>
                  <el-col :span="11">
                    <el-form-item label="法人" prop="legalPerosn">
                      <file-tip content="法人"></file-tip>
                      <el-input type="input" v-model="businessRegistration.business.legalPerosn"
                                placeholder="法人" clearable autosize
                                resize="both" tabindex=5
                                maxlength=10
                                disabled="disabled"
                      ></el-input>
                    </el-form-item>
                  </el-col>
                  <el-col :span="11">
                    <el-form-item label="联系人" prop="contact">
                      <file-tip content="联系人"></file-tip>
                      <el-input type="input" v-model="businessRegistration.business.contact"
                                placeholder="联系人" clearable autosize
                                resize="both" tabindex=6
                                maxlength=10
                                disabled="disabled"
                      ></el-input>
                    </el-form-item>
                  </el-col>
                  <el-col :span="11">
                    <el-form-item label="联系人电话" prop="contactPhone">
                      <file-tip content="联系人电话"></file-tip>
                      <el-input type="input" v-model="businessRegistration.business.contactPhone"
                                placeholder="联系人电话" clearable autosize
                                resize="both" tabindex=7
                                maxlength=11
                                disabled="disabled"
                      ></el-input>
                    </el-form-item>
                  </el-col>
                  <el-col :span="11">
                    <el-form-item label="省市区">
                      <el-input type="input" v-model="selectedBusinessAddresses"
                                placeholder="省市区" clearable autosize
                                resize="both" tabindex=7
                                maxlength=11
                                disabled="disabled"
                      ></el-input>
                    </el-form-item>
                  </el-col>
                  <el-col :span="11">
                    <el-form-item label="商铺详细地址" prop="businessAddress">
                      <file-tip content="商铺详细地址"></file-tip>
                      <el-input type="input" v-model="businessRegistration.business.businessAddress"
                                placeholder="商铺详细地址" clearable autosize
                                resize="both" tabindex=11
                                maxlength=100
                                disabled="disabled"
                      ></el-input>
                    </el-form-item>
                  </el-col>
                  <el-col :span="11">
                    <el-form-item label="身份证号码" prop="idNumber">
                      <file-tip content="身份证号码"></file-tip>
                      <el-input type="input" v-model="businessRegistration.business.idNumber"
                                placeholder="身份证号码" clearable autosize
                                resize="both" tabindex=12
                                maxlength=18
                                disabled="disabled"
                      ></el-input>
                    </el-form-item>
                  </el-col>
                  <el-col :span="11">
                    <el-form-item label="营业执照号码" prop="licenseNumber">
                      <file-tip content="营业执照号码"></file-tip>
                      <el-input type="input" v-model="businessRegistration.business.licenseNumber"
                                placeholder="营业执照号码" clearable autosize
                                resize="both" tabindex=13
                                maxlength=60
                                disabled="disabled"
                      ></el-input>
                    </el-form-item>
                  </el-col>
                  <el-col :span="11">
                    <el-form-item label="主营产品" prop="mainProducts">
                      <file-tip content="主营产品"></file-tip>
                      <el-input type="input" v-model="businessRegistration.business.mainProducts"
                                placeholder="主营产品" clearable autosize
                                resize="both" tabindex=14
                                maxlength=64
                                disabled="disabled"
                      ></el-input>
                    </el-form-item>
                  </el-col>
                  <el-col :span="11">
                    <el-form-item label="营业开始时间" prop="beginHours">
                      <el-time-select clearable
                                      v-model="businessRegistration.business.beginHours"
                                      align="right"
                                      placeholder="营业时间"
                                      :picker-options="{
                                                   start: '08:30',
                                                   step: '00:15',
                                                   end: '18:30'
                                                }"
                                      disabled="disabled">
                      </el-time-select>
                    </el-form-item>
                  </el-col>
                  <el-col :span="11">
                    <el-form-item label="营业结束时间">
                      <el-time-select clearable
                                      v-model="businessRegistration.business.endHours"
                                      align="right"
                                      placeholder="营业结束时间"
                                      :picker-options="{
                                                   start: '08:30',
                                                   step: '00:15',
                                                   end: '18:30'
                                                }"
                                      disabled="disabled">
                      </el-time-select>
                    </el-form-item>
                  </el-col>
                  <el-col :span="11">
                    <el-form-item label="qq号码" prop="qqNumber">
                      <file-tip content="qq号码"></file-tip>
                      <el-input type="input" v-model="businessRegistration.business.qqNumber"
                                placeholder="qq号码" clearable autosize
                                resize="both" tabindex=17
                                maxlength=12
                                disabled="disabled">
                      </el-input>
                    </el-form-item>
                  </el-col>
                  <el-col :span="11">
                    <el-form-item label="邮箱" prop="email">
                      <file-tip content="邮箱"></file-tip>
                      <el-input type="input" v-model="businessRegistration.business.email"
                                placeholder="邮箱" clearable autosize
                                resize="both" tabindex=18
                                maxlength=30
                                disabled="disabled"
                      ></el-input>
                    </el-form-item>
                  </el-col>
                  <el-col :span="11">
                    <el-form-item label="注册时间" prop="registrationTime">
                      <el-date-picker clearable
                                      v-model="businessRegistration.business.registrationTime"
                                      align="right"
                                      type="date"
                                      placeholder="注册时间"
                                      format="yyyy-MM-dd"
                                      value-format="yyyy-MM-dd"
                                      disabled="disabled">
                      </el-date-picker>
                    </el-form-item>
                  </el-col>
                  <el-col :span="11">
                    <el-form-item label="商家描述" prop="description">
                      <file-tip content="商家描述"></file-tip>
                      <el-input type="input" v-model="businessRegistration.business.description"
                                placeholder="商家描述" clearable autosize
                                resize="both" tabindex=21
                                maxlength=500
                                disabled="disabled"
                      ></el-input>
                    </el-form-item>
                  </el-col>
                  <el-col :span="20">
                    <el-form-item label="审核资料">
                      <div v-for="file in fileList">
                        <img height="150px" width="200" style="text-align: center;margin-left: 0px"
                             :src="headerFile+file.attachmentAddr" alt="">
                        <el-row>
                   <span style="color: blue;cursor: pointer"
                         @click="down(file.attachmentAddr)">下载</span> <span
                          style="color: blue;cursor: pointer" @click="view(file.attachmentAddr)">预览</span>
                        </el-row>
                      </div>
                    </el-form-item>
                  </el-col>
                                <el-col :span="11">
                                    <el-form-item label="审核时间" prop="reviewTime">
                                        <el-date-picker clearable
                                                v-model="businessRegistration.reviewTime"
                                                align="right"
                                                type="date"
                                                placeholder="审核时间"
                                                :picker-options="pickerOptionsReviewTime">
                                        </el-date-picker>
                                    </el-form-item>
                                </el-col>
                  <el-col :span="11">
                    <el-form-item label="审核人" prop="reviewer">
                      <file-tip content="审核人"></file-tip>
                      <el-input type="input" v-model="businessRegistration.reviewer"
                                placeholder="审核人" clearable autosize
                                resize ="both" tabindex=4
                                maxlength=50
                      ></el-input>
                    </el-form-item>
                  </el-col>
                                <el-col :span="11">
                                        <el-form-item label="审核意见" prop="reviewAdvice">
                                            <file-tip content="审核意见"></file-tip>
                                            <el-input type="input" v-model="businessRegistration.reviewAdvice"
                                                      placeholder="审核意见" clearable autosize
                                                      resize ="both" tabindex=6
                                                              maxlength=500
                                            ></el-input>
                                    </el-form-item>
                                    </el-col>
                                <el-col :span="11">
                                    <el-form-item label="审核状态" prop="reviewStatus">
                                        <el-autocomplete
                                                class="inline-input"
                                                value-key="name"
                                                v-model="businessRegistration.reviewStatus"
                                                :fetch-suggestions="searchReviewStatus "
                                                placeholder="审核状态"
                                                clearable autosize
                                                resize ="both" tabindex="7"
                                        ></el-autocomplete>
                                    </el-form-item>
                                </el-col>
                                <!--<el-col :span="22">-->
                                    <!--<el-form-item label="备注" prop="remark">-->
                                        <!--<file-tip content="备注"></file-tip>-->
                                            <!--<el-input type="textarea" v-model="businessRegistration.remark"-->
                                                      <!--placeholder="备注" clearable autosize-->
                                                      <!--resize ="both" tabindex=10000-->
                                                              <!--maxlength=255-->
                                            <!--&gt;</el-input>-->
                                    <!--</el-form-item>-->
                                <!--</el-col>-->
                    <el-col :span="11">
                        <el-form-item>
                            <el-button type="primary" @click="submitBusinessRegistration()"    :loading="isSubmiting" v-permission:simple_businessRegistration_BusinessRegistration_Edit >提交</el-button>
                        </el-form-item>
                    </el-col>
                </el-form>
            </div>
            <div class="text item clearfix">
                <el-alert v-for="remark in alerts.remarks"  :key="remark.title"
                          v-bind:title="remark.title"
                          type="info"
                          v-bind:description="remark.content">
                </el-alert>
            </div>
        </el-card>
    </div>
</template>

<script>





import {BusinessService} from '../../simple/businessRegistration/BusinessService'
import {BusinessRegistrationService} from './BusinessRegistrationService'
import {d4utils} from '../../../tools/d4utils'
import fileTip from '@/components/tip/fileTip'


export default {
    components: {
        fileTip,
    },
    data() {
        var validateIntRange = d4utils.validateFloatRange;
        var validateFloatRange = d4utils.validateFloatRange;
        var validateString = d4utils.validateString;

        return {
          headerFile: process.env.BASE_FILE_VIEW,
          fileList: null,
          formName: "simple_business",
          files: [],
            rules: {
                name: [
                    {required:  false , message: '请输入名称', trigger: 'blur'},
                    { validator:validateString(0,1000,/^.*$/,"输入的数据不正确，请检查"), trigger: 'blur' },
                    ],
                businessId: [
                    {required:  false , message: '请输入商家', trigger: 'blur'},
                    ],
                businessName: [
                    {required:  false , message: '请输入商家', trigger: 'blur'},
                    ],
                reviewer: [
                    {required:  false , message: '请输入审核人', trigger: 'blur'},
                    { validator:validateString(0,1000,/^.*$/,"输入的数据不正确，请检查"), trigger: 'blur' },
                    ],
                reviewTime: [
                    {
                      required:  false , message: '请输入审核时间', trigger: 'change'},
                    ],
                reviewAdvice: [
                    {required:  false , message: '请输入审核意见', trigger: 'blur'},
                    { validator:validateString(0,1000,/^.*$/,"输入的数据不正确，请检查"), trigger: 'blur' },
                    ],
                reviewStatus: [
                    {required:  false , message: '请输入审核状态', trigger: 'blur'},
                    ],
                remark: [
                    {required:  false , message: '请输入备注', trigger: 'blur'},
                    { validator:validateString(0,1000,/^.*$/,"输入的数据不正确，请检查"), trigger: 'blur' },
                    ],
           },
            isSubmiting: false,
            business: {
              businessName: null
            },
            businessRegistration:{},
            businessRegistrationId:null,
          //current表示当前页面，0为编辑页面，1为新增页面
            current: 0,
            pickerOptionsReviewTime: {
                disabledDate(time) {
                    //TODO: 请在此判断可以输入的日期范围,
                    //return time.getTime() > Date.now();
                    return false;
                },
                shortcuts: [{
                    text: '今天',
                    onClick(picker) {
                        picker.$emit('pick', new Date());
                    }
                }, {
                    text: '昨天',
                    onClick(picker) {
                        const date = new Date();
                        date.setTime(date.getTime() - 3600 * 1000 * 24);
                        picker.$emit('pick', date);
                    }
                }, {
                    text: '一周前',
                    onClick(picker) {
                        const date = new Date();
                        date.setTime(date.getTime() - 3600 * 1000 * 24 * 7);
                        picker.$emit('pick', date);
                    }
                }]
            },
            pickerOptionsCreateDatetime: {
                disabledDate(time) {
                    //TODO: 请在此判断可以输入的日期范围,
                    //return time.getTime() > Date.now();
                    return false;
                },
                shortcuts: [{
                    text: '今天',
                    onClick(picker) {
                        picker.$emit('pick', new Date());
                    }
                }, {
                    text: '昨天',
                    onClick(picker) {
                        const date = new Date();
                        date.setTime(date.getTime() - 3600 * 1000 * 24);
                        picker.$emit('pick', date);
                    }
                }, {
                    text: '一周前',
                    onClick(picker) {
                        const date = new Date();
                        date.setTime(date.getTime() - 3600 * 1000 * 24 * 7);
                        picker.$emit('pick', date);
                    }
                }]
            },
            pickerOptionsUpdateDatetime: {
                disabledDate(time) {
                    //TODO: 请在此判断可以输入的日期范围,
                    //return time.getTime() > Date.now();
                    return false;
                },
                shortcuts: [{
                    text: '今天',
                    onClick(picker) {
                        picker.$emit('pick', new Date());
                    }
                }, {
                    text: '昨天',
                    onClick(picker) {
                        const date = new Date();
                        date.setTime(date.getTime() - 3600 * 1000 * 24);
                        picker.$emit('pick', date);
                    }
                }, {
                    text: '一周前',
                    onClick(picker) {
                        const date = new Date();
                        date.setTime(date.getTime() - 3600 * 1000 * 24 * 7);
                        picker.$emit('pick', date);
                    }
                }]
            },
        alerts:{ remarks:[{title:"功能说明",content:"TODO: 请在这里加上功能说明"},],
                successes:[], //TODO:[{title:'消息内容'},]
                infos:[],
                warnings:[],
                errors:[]},
          selectedBusinessAddresses: [],
          businessAddresses: null
        }
    },

    methods: {
      view(path) {
        window.open(process.env.BASE_FILE_VIEW + "" + path + '&isOnline=true')
      },
      down(path) {
        window.open(process.env.BASE_FILE_VIEW + "" + path)
      },
        addAlert(message,title,type){
            //type 可选的值为:remarks,successes,infos,warnings,errors
            type = type? type:"errors";
            type = this.alerts[type]?type:"errors";
            title = title?title:message;
            this.alerts[type].unshift({title:title,content:message});
        },
        removeAlert(title,type){
            //type 可选的值为:remarks,successes,infos,warnings,errors
            type = type? type:"errors";
            type = this.alerts[type]?type:"errors";
            for (var i = this.alerts[type].length-1; i>=0 ; i--) {
                delete this.alerts[type][i]
            }
        },
        submitBusinessRegistration() {
            var refs = this.$refs;
            refs['businessRegistrationForm'].validate(valid => {
            if (valid) {
            if(this.businessRegistration.eid)//编辑商家注册
            {
            this.updateBusinessRegistration();
            }
            else//保存商家注册
            {
            this.saveBusinessRegistration();
            }
            } else {
            return false
            }
            })
        },
        saveBusinessRegistration()//保存商家注册
        {
            this.isSubmiting = true;
            this.buttonRequestProgressStart("正在保存,请稍后...");
            BusinessRegistrationService.saveBusinessRegistration(this.businessRegistration).then((resp) => {
                this.buttonRequestProgressClose();
                this.isSubmiting = false;
                var router = this.$router;
                router.push({path: '/simple/businessRegistration/businessRegistration'})
        }).catch((error) => {
            this.buttonRequestProgressClose();
            //error的data属性是后台传入的数据,data的extendedData存有后台传入的数据
            this.isSubmiting = false;

            this.addAlert({title:error.data.message})
            this.$message({
                type: 'error',
                message: '保存出错,原因是：'+error.data.message
            })
        })
        },
        updateBusinessRegistration()//编辑商家注册
        {
            this.isSubmiting = true;
            this.buttonRequestProgressStart("正在更新,请稍后...");
            BusinessRegistrationService.updateBusinessRegistration(this.businessRegistration).then((resp) => {
                this.buttonRequestProgressClose();
                this.isSubmiting = false;
                var router = this.$router;
                router.push({path: '/simple/businessRegistration/businessRegistration'})
        }).catch((error) => {
            this.buttonRequestProgressClose();
            //error的data属性是后台传入的数据,data的extendedData存有后台传入的数据
            this.isSubmiting = false;
            this.addAlert({title:error.data.message})
            this.$message({
                type: 'error',
                message: '保存出错,原因是：'+error.data.message
            })
        })
        },
        findBusinessRegistrationForEdit(businessRegistrationId)//查找商家注册
        {
            BusinessRegistrationService.findBusinessRegistrationForEdit(businessRegistrationId).then((resp) => {
                this.prepareForEdit(resp.data);
        }).catch((error) => {
            this.$message({
            type: 'error',
            message: '查询商家注册出错'
        })
        })
        },
        createBusinessRegistration()//创建新的商家注册
        {
            BusinessRegistrationService.createBusinessRegistration().then((resp) => {
                this.prepareForEdit(resp.data);
        }).catch((error) => {
            this.$message({
            type: 'error',
            message: '创建新的商家注册出错'
        })
        })
        },
        prepareForEdit(businessRegistrationEditDto)
        {
            this.businessRegistration = businessRegistrationEditDto.businessRegistration;
            this.reviewStatusCodeTables = businessRegistrationEditDto.reviewStatusCodeTables;
            this.businessBusinesss = businessRegistrationEditDto.businessBusinesss
            this.business = businessRegistrationEditDto.businessRegistration.business
            this.findBusinessForView(this.business.eid)
            this.selectedBusinessAddresses=this.business.provincesName+this.business.cityName+this.business.areaName;
            console.log("address",this.selectedBusinessAddresses)
        },
      findBusinessForView(businessId)//查找商家
      {
        BusinessService.findBusinessForView(businessId).then((resp) => {
          this.business = resp.data;
          BusinessService.findAllUploadedFilesByIdAndName(businessId, this.formName)
            .then((res) => {
              this.fileList = res.data;
              this.fileList.forEach((file) => {
                BusinessService.downloadAttachments(false, file.attachmentAddr)
                  .then((res) => {
                    console.log("附件下载成功")
                    let File = {};
                    File = file;
                    this.files.push(file);
                  })
                  .catch((err) => {
                    console.log("附件下载失败")
                  })
              })
              console.log("附件读取成功：" + JSON.stringify(this.files))

            })
            .catch((err) => {
              console.log("附件读取失败")
            })
        }).catch((error) => {
          this.$message({
            type: 'error',
            message: '查询商家出错'
          })
        })
      },
        searchReviewStatus(queryString, cb) {
            var reviewStatusCodeTables = this.reviewStatusCodeTables;
            var results = queryString ? reviewStatusCodeTables.filter(this.createFilterReviewStatus(queryString)) : reviewStatusCodeTables;
            cb(results);
        },
        createFilterReviewStatus(queryString) {
            return (ReviewStatus) => {
                return (ReviewStatus.name.toLowerCase().indexOf(queryString.toLowerCase()) >= 0);
            };
        },

        searchBusinessName(queryString, cb) {
            var businessBusinesss = this.businessBusinesss;

            //如果不为空，说明在加载页面的时候已经加载了所有的外键数据，否则表示没加载，则根据输入的信息从后台加载
            if(businessBusinesss)
            {
                var results = queryString ? businessBusinesss.filter(this.createFilterBusinessName(queryString)) : businessBusinesss;
                cb(results);
            }
            else
            {
                var results = d4utils.getObjectCached("findBusinesssWithIdNameByName_"+queryString);
                if(results)
                {
                    cb(results);
                }
                else
                {
                    var cb1 = cb;
                    BusinessService.findBusinesssWithIdNameByName(queryString).then((resp) => {
                    if (resp.data)
                    {
                        var results = resp.data;
                        d4utils.cacheObject("findBusinesssWithIdNameByName_"+queryString);
                        cb1(results)
                    }
                else
                    {
                        this.$message({
                            type: 'error',
                            message: '没有查询到引用数据'
                        });
                    }
                }).catch((error) => {
                    this.$message({
                    type: 'error',
                    message: '查询引用数据出错'
                })
                });
                }
            }
        },
            createFilterBusinessName(queryString) {
                return (business) => {
                    return (business.name.toLowerCase().indexOf(queryString.toLowerCase()) >= 0);
                };
            },
            handleSelectBusinessName(item){
                this.businessRegistration.businessId = item.eid;
            },
    },
    created() {
        this.businessRegistrationId = this.$route.params.businessRegistrationId;
        if(this.businessRegistrationId)//编辑
        {
          this.findBusinessRegistrationForEdit(this.businessRegistrationId);
        }
        else//新增
        {
             this.createBusinessRegistration();
        }
    },
}
</script>
<style scoped lang="scss">

</style>
